package com.pr.makfilm.server;

import com.pr.makfilm.admin.pages.user.list.UserListPage;
import com.pr.makfilm.core.cmp.authorization.UserAuthorities;
import com.pr.makfilm.security.service.ISecurityService;
import com.pr.makfilm.server.pages.LoginPage;
import com.pr.makfilm.user.pages.school.list.SchoolListPage;
import org.apache.wicket.Page;
import org.apache.wicket.RuntimeConfigurationType;
import org.apache.wicket.authroles.authentication.AbstractAuthenticatedWebSession;
import org.apache.wicket.authroles.authentication.AuthenticatedWebApplication;
import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AnnotationsRoleAuthorizationStrategy;
import org.apache.wicket.core.util.resource.ClassPathResourceFinder;
import org.apache.wicket.injection.Injector;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.response.filter.ServerAndClientTimeFilter;
import org.apache.wicket.settings.IExceptionSettings;
import org.apache.wicket.settings.IRequestCycleSettings;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import org.apache.wicket.util.file.IResourceFinder;
import org.apache.wicket.util.lang.Bytes;
import org.wicketstuff.annotation.scan.AnnotatedMountScanner;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * User: Pawel
 * Date: 07.09.13
 * Time: 16:23
 * To change this template use File | Settings | File Templates.
 */
public class MainApplication extends AuthenticatedWebApplication
{
   private static boolean initialized = false;

   @SpringBean
   private ISecurityService securityService;

   @Override
   protected Class<? extends AbstractAuthenticatedWebSession> getWebSessionClass()
   {
      return MainSession.class;
   }

   @Override
   protected void init()
   {
      if (!initialized)
      {
         configureApplicationSettings();
         new AnnotatedMountScanner().scanPackage("com.pr.makfilm.server").mount(this);
         new AnnotatedMountScanner().scanPackage("com.pr.makfilm.admin").mount(this);
         new AnnotatedMountScanner().scanPackage("com.pr.makfilm.user").mount(this);
         Injector.get().inject(this);
         initialized = true;
      }
   }

   @Override
   protected Class<? extends WebPage> getSignInPageClass()
   {
      return LoginPage.class;
   }

   @Override
   public Class<? extends Page> getHomePage()
   {
      if (AuthenticatedWebSession.get().isSignedIn())
      {
         if (securityService.hasRole(UserAuthorities.ADMIN))
         {
            return UserListPage.class;
         }
         else
         {
            return SchoolListPage.class;
         }
      }
      return LoginPage.class;
   }

   protected void configureApplicationSettings()
   {
      getSecuritySettings().setAuthorizationStrategy(new AnnotationsRoleAuthorizationStrategy(this));
      getSecuritySettings().setUnauthorizedComponentInstantiationListener(this);
      getRequestCycleSettings().setResponseRequestEncoding("UTF-8");
      getMarkupSettings().setDefaultMarkupEncoding("UTF-8");
      getResourceSettings().setResourceFinders(Arrays.<IResourceFinder>asList(new ClassPathResourceFinder("")));
//      getResourceSettings().setCachingStrategy(NoOpResourceCachingStrategy.INSTANCE);

      getComponentInstantiationListeners().add(new SpringComponentInjector(this));
      getRequestCycleSettings().setRenderStrategy(IRequestCycleSettings.RenderStrategy.ONE_PASS_RENDER);
      getRequestCycleSettings().addResponseFilter(new ServerAndClientTimeFilter());
      getMarkupSettings().setStripWicketTags(true);
      getMarkupSettings().setCompressWhitespace(true);

      if (getConfigurationType() == RuntimeConfigurationType.DEPLOYMENT)
      {
         getDebugSettings().setAjaxDebugModeEnabled(false);
         getExceptionSettings().setUnexpectedExceptionDisplay(IExceptionSettings.SHOW_INTERNAL_ERROR_PAGE);
      }
      getApplicationSettings().setDefaultMaximumUploadSize(Bytes.megabytes(256L)); // max upload 1M
      getApplicationSettings().setUploadProgressUpdatesEnabled(true);
   }

   @Override
   public RuntimeConfigurationType getConfigurationType()
   {
      return RuntimeConfigurationType.DEPLOYMENT;
   }
}
